﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using dataSend;
using System.Threading;

namespace dataSendModel
{
    public  class Waitting
    {
        BackgroundWorker bgw = null;
        private int time;
        //private Thread thread;
        public Waitting(){}
        public Waitting(int time)
        {
            this.time = time;
        }
        public  void showWatting()
        {
            WaittingForm wf = new WaittingForm();
            wf.Show();
            bgw = new BackgroundWorker();
            bgw.WorkerSupportsCancellation = true; //设置能否取消任务
            bgw.WorkerReportsProgress = true;
            bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
            bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(wf.closeForm); //完成后关闭窗体
            bgw.ProgressChanged += new ProgressChangedEventHandler(wf.progessChange); //显示进度条
            bgw.RunWorkerAsync();
        }

        //让dowork去做的事
        void  bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = workTimer(e);
        }

        //设置WaittingForm显示进度条
        private int workTimer(DoWorkEventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                if (bgw.CancellationPending)
                {
                    e.Cancel = true;
                    return -1;
                }
                else
                {
                    this.bgw.ReportProgress(i * 10);
                }
                Thread.Sleep(time);
            }
            return -1;
        }


    }
}
